<?php
/**
 * @file designcontroller.php $
 * @author yuebin (yuebin@haibao.com) $
 * @date 2015年12月4日 11:30:41 $
 * @brief  $
 */
namespace haibao\cms\data\control;

class DesignController extends \haibao\cms\data\BaseMysql {

	public function __construct(){
		parent::__construct('\haibao\cms\model\data\Base');
	}
	
	public function getBrandImgById($brandsArr){
		$brandImgIdArr = $brandImgUrlArr = array();
		$str = preg_replace('/\d+/', '%s', implode(',', $brandsArr));
		$sql = 'select Id,ImgId from Brand where Id in ('.$str.')';
		$imgIdResult = $this->query($sql,$brandsArr);
		while($row = $imgIdResult->fetch_assoc()){
			$brandImgIdArr[$row['Id']] = $row['ImgId'];
		}
		$images = \haibao\cms\business\ImageLibrary::getImageUrlById($brandImgIdArr);
		foreach($brandImgIdArr as $key=>$val){
			if(isset($images[$val])){
				$brandImgUrlArr[$key] = $images[$val];
			}else{
				$brandImgUrlArr[$key] = '';
			}
		}
		return $brandImgUrlArr;
	}
	
	/**
	 * 根据设计师Id获取粉丝数
	 * @param $preFiveDesignerIdArr 设计师ID数组
	 */
	public function getDesignerFansCount($preFiveDesignerIdArr){
		$designerFansCount = array();
		$str = preg_replace('/\d+/', '%s', implode(',', $preFiveDesignerIdArr));
		$sql = 'select Id,DesignerId,FansNum from DesignerProperty where DesignerId in ('.$str.')';
		$designerResult = $this->query($sql,$preFiveDesignerIdArr);
		while($row = $designerResult->fetch_assoc()){
			$designerFansCount[$row['DesignerId']] = $row;
		}
		//print_r($designerFansCount);exit;
		return $designerFansCount;
	}
	
	/**
	 * 根据明星首字母获取数据
	 */
	public function getDesignListByLetter($firstLetter,$limit=14){
		$designerList = array();
		if($firstLetter == 'OTHER'){
			$str='';
			for($I=ord('a');$I<=ord('z');$I++){
				$str .= "'".chr($I)."',";
			}
			$str = substr($str,0,-1);
		}
		if($firstLetter == 'OTHER'){
			$sql = "select a.Id,a.TagId,a.NameCn,a.NameEn,a.ImgId,b.EditFansNum as FansCount FROM `Designer` as a left join `DesignerProperty` as b on a.Id = b.DesignerId WHERE a.STATUS = %s and a.PinyinEN not in ($str) order by b.FansNum desc limit %s";
			$result = $this->query($sql,array(\haibao\cms\model\data\designer\Designer::STATUS_ENABLE,$limit));
		}else{
			$sql = "SELECT a.Id,a.TagId,a.NameCn,a.NameEn,a.ImgId,b.EditFansNum as FansCount FROM `Designer` as a left join `DesignerProperty` as b on a.Id = b.DesignerId WHERE a.STATUS = %s AND a.PinyinEN = %s ORDER BY b.FansNum DESC LIMIT %s";
			$result = $this->query($sql,array(\haibao\cms\model\data\designer\Designer::STATUS_ENABLE,$firstLetter,$limit));
		}
		while($row = $result->fetch_assoc()){
			array_push($designerList,$row);
		}
		return $designerList;
	}
	
	/**
	 * 获取品牌库首页——大牌设计师
	 * @param unknown $limit
	 */
	public function getDesigner($limit){
	    $designerList = array();
	    $sql="SELECT
                q70.`Id`,
            	q70.`TagId`,
            	q70.`TopicId`,
            	q70.`NameCN` ,
            	q70.`NameEN`,
            	q70.`ImgId`,
            	q70.`UpdateTime`,
            	q70.`IsBrand`,
            	q70.`Status`,
            	 q71.`DesignerId`,
            	q71.`BrandId`,
            	q71.`RelType`
            FROM
            	`Designer` q70
            LEFT JOIN `DesignerRelBrand` q71 ON q70.`Id` = q71.`DesignerId`
            WHERE
            	q70.`Status` = 1
            GROUP BY
            	q70.`Id`
            ORDER BY
            	q70.`UpdateTime` DESC,
            	q71.`RelType` DESC
            LIMIT ".$limit;
	    $result =  $this->query($sql);
	    while($row = $result->fetch_assoc()){
	        array_push($designerList,$row);
	    }
	    return $designerList;
	}
	
	public function getDesignIds(){
	    $sql = 'select Id from Designer where Status = 1';
	    $result = $this->query($sql);
	    $designerIds = array();
	    while($row = $result->fetch_assoc()){
	        $designerIds[] = $row['Id'];
	    }
	    return $designerIds;
	}
}